package com.banciyuan.bcywebview.biz.circles.smooth.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.biz.circles.smooth.fragment.CircleSmoothUserTopFragment;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.ActiveUser;
import de.greenrobot.daoexample.model.PostCircleRank;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/4/14.
 */

public class TopUserHolder extends RecyclerView.ViewHolder {

    private View userLine;
    private TextView[] names;
    private ImageView[] headers;
    private View[] userContainer;

    public TopUserHolder(View itemView) {
        super(itemView);
        userLine = itemView.findViewById(R.id.user_line);
        names = new TextView[]{(TextView) itemView.findViewById(R.id.user_name_one),
                (TextView) itemView.findViewById(R.id.user_name_two),
                (TextView) itemView.findViewById(R.id.user_name_three)};
        headers = new ImageView[]{(ImageView) itemView.findViewById(R.id.user_pic_one),
                (ImageView) itemView.findViewById(R.id.user_pic_two),
                (ImageView) itemView.findViewById(R.id.user_pic_three)};
        userContainer = new View[]{itemView.findViewById(R.id.user_info_one),
                itemView.findViewById(R.id.user_info_two),
                itemView.findViewById(R.id.user_info_three)};
    }

    public void renderUser(final Context mContext, Timeline mItem) {
        ((View) (userLine.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseObserver.getInstance().notifyWatchers(BaseObserver.CIRCLE_GOTOPUSER);
            }
        });

        PostCircleRank postCircleRank = mItem.getPost_rank();
        if (postCircleRank != null) {
            List<ActiveUser> activeUserList = new ArrayList<>();
            activeUserList.add(postCircleRank.getDrawer());
            activeUserList.add(postCircleRank.getWriter());
            activeUserList.add(postCircleRank.getCoser());
            for (int i = 0; i < activeUserList.size(); i++) {
                if (activeUserList.get(i) != null) {
                    final ActiveUser a = activeUserList.get(i);
                    userContainer[i].setVisibility(View.VISIBLE);
                    headers[i].setVisibility(View.VISIBLE);
                    names[i].setText(String.format(mContext.getString(R.string.rank_one_format), a.getUname()));
                    ImageLoader.getInstance().displayImage(a.getAvatar(), headers[i], ImageOptions.optionHead);
                    final int finalI = i;
                    headers[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (finalI){
                                case 0:
                                    BaseObserver.getInstance().notifyWatchers(BaseObserver.CIRCLE_GOTOPUSER_INNER, CircleSmoothUserTopFragment.DRAWER);
                                    break;
                                case 1:
                                    BaseObserver.getInstance().notifyWatchers(BaseObserver.CIRCLE_GOTOPUSER_INNER, CircleSmoothUserTopFragment.WRITER);
                                    break;
                                case 2:
                                    BaseObserver.getInstance().notifyWatchers(BaseObserver.CIRCLE_GOTOPUSER_INNER, CircleSmoothUserTopFragment.COSER);
                                    break;
                            }
                        }
                    });
                } else {
                    userContainer[i].setVisibility(View.GONE);
                    headers[i].setVisibility(View.GONE);
                }
            }
        }
    }

}
